
name   := arch
id     := 1000
rootfs := /mnt/archlinux

define mountlist
	echo "--rbind $(3) $(1) $(2)"
endef


define umountlist
	echo "$(2)"
endef



define append
	bindlist += $(shell echo "$(1),$(2),$(3)" | sed "s/ //g")
endef



setbind:
	$(eval $(call append, /run/user/$(id)/pulse       ,$(rootfs)/run/user/$(id)/pulse         ,    ))
	$(eval $(call append, /run/dbus/system_bus_socket ,$(rootfs)/run/dbus/system_bus_socket   ,    ))
	$(eval $(call append, /dev                        ,$(rootfs)/dev                          ,    ))
	$(eval $(call append, /tmp                        ,$(rootfs)/tmp                          ,-rw ))
	$(eval $(call append, $$HOME/.themes              ,$(rootfs)/home/$(name)/.themes         ,-r  ))
	$(eval $(call append, $$HOME/.icons               ,$(rootfs)/home/$(name)/.icons          ,-r  ))
	$(eval $(call append, $$HOME/.fonts               ,$(rootfs)/home/$(name)/.fonts          ,-r  ))
	$(eval $(call append, $$HOME/.gtkrc-2.0           ,$(rootfs)/home/$(name)/.gtkrc-2.0      ,-r  ))
	$(eval $(call append, $$HOME/.config/gtk-2.0      ,$(rootfs)/home/$(name)/.config/gtk-2.0 ,-r  ))
	$(eval $(call append, $$HOME/.config/gtk-3.0      ,$(rootfs)/home/$(name)/.config/gtk-3.0 ,-r  ))



bindlist += /run/user/$(id)/pulse,$(rootfs)/run/user/$(id)/pulse
bindlist += /run/dbus/system_bus_socket,$(rootfs)/run/dbus/system_bus_socket,
bindlist += /dev,$(rootfs)/dev,
bindlist += /tmp,$(rootfs)/tmp,-rw,--bind
bindlist += $$HOME/.themes,$(rootfs)/home/$(name)/.themes,-r
bindlist += $$HOME/.icons,$(rootfs)/home/$(name)/.icons,-r
bindlist += $$HOME/.fonts,$(rootfs)/home/$(name)/.fonts,-r
bindlist += $$HOME/.gtkrc-2.0,$(rootfs)/home/$(name)/.gtkrc-2.0,-r
bindlist += $$HOME/.config/gtk-2.0,$(rootfs)/home/$(name)/.config/gtk-2.0,-r
bindlist += $$HOME/.config/gtk-3.0,$(rootfs)/home/$(name)/.config/gtk-3.0,-r


mount: 
	@for v in $(bindlist);do \
		origin=`echo $$v | awk -F ',' '{print $$1}'`; \
		mountpoint=`echo $$v | awk -F ',' '{print $$2}'`; \
		param=`echo $$v | cut -d',' -f3- | sed 's/,/ /g'`; \
		echo "mount $$param $$origin $$mountpoint"; \
	done


umount: 
	@for v in $(bindlist);do \
		echo $$v; \
	done


env += DBUS_SESSION_BUS_ADDRESS=$$DBUS_SESSION_BUS_ADDRESS
env += XDG_RUNTIME_DIR=$$XDG_RUNTIME_DIR

test:
	@echo $$HOME
